<template>
  <div>
    <el-form label-width="80px" :model="formLabelAlign" :rules="rules" ref="updatePassword">
      <el-form-item label="密码" prop="password">
        <el-input v-model="formLabelAlign.password" type="password" placeholder="请输入修改的密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="repassword">
        <el-input v-model="formLabelAlign.repassword" type="password" placeholder="请再次输入密码"></el-input>
      </el-form-item>
    </el-form>
    <div>
      <el-button type="primary" style="float: right" @click="updatePassword">确认修改</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "UpdatePassword",
  data() {
    return {
      formLabelAlign: {
        password: '',
        repassword: '',
        user: ''
      },
      rules: {
        password: [
          {
           min:6, required: true,
            message: '最少6位', trigger: 'blur'
          }],

        repassword: [{
          min:6, required: true,
          message: '最少6位', trigger: 'blur'
        }]
      }

    }},
  methods:
    {
      updatePassword(){
        this.$confirm('确认修改密码?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$refs.updatePassword.validate((valid) => {
            if (valid) {
              if(this.formLabelAlign.password != this.formLabelAlign.repassword){
                this.$message("两次输入的密码不一致");
              }else {
                this.putRequest(this.$store.state.baseUrl+'/user/updatePassword',{
                  password:this.formLabelAlign.password,
                  user_id:this.user.user_id
                }).then(resp => {
                  this.$message({
                    type: 'success',
                    message: '修改成功!'
                  });
                  this.$router.push("/");
                })

              }
            } else {
              this.$message({
                type:'error',
                message:'密码格式错误'
              })
              return false;
            }
          });

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消修改'
          });
        });
      }
    }
  ,
    mounted()
    {
      this.user = JSON.parse(window.sessionStorage.getItem("user"));
    }

  }
</script>

<style scoped>

</style>
